package cn.com.postek;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.postek.cdf.CDFPTKAndroid;
import com.postek.cdf.CDFPTKAndroidImpl;

import java.util.HashMap;
import java.util.Map;

import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniModule;

public class POSTEKPrint extends UniModule {
    private static final String TAG = "POSTEKPrint";
    public static CDFPTKAndroid cdf = null;
    private boolean isConnected = false;

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            if (msg.what == CDFPTKAndroid.PTK_MSG_WHAT_WIFICONNECT) {
                if (msg.arg1 == 0) {
                    isConnected = true;
                    Map<String, Object> params = new HashMap<>();
                    params.put("msg", "Connect successful");
                    params.put("code", 1);
                    mUniSDKInstance.fireGlobalEventCallback("PrintEvent", params);
                } else {
                    isConnected = false;
                    Map<String, Object> params = new HashMap<>();
                    params.put("msg", "Disconnect");
                    params.put("code", -1);
                    mUniSDKInstance.fireGlobalEventCallback("PrintEvent", params);
                }
            }
        }
    };

    //2.1 链接WIFI
    @UniJSMethod(uiThread = false)
    public void PTK_ConnectWiFi(JSONObject options, UniJSCallback callback) {
        if (cdf != null) {
            try {
                cdf.PTK_CloseConnectWiFi();
                cdf = null;
            } catch (Exception e) {
                Log.d(TAG, "PTK_ConnectWiFi: " + e);
            }
        }

        try {
            String ip = options.getString("ip");
            int port = options.getIntValue("port");
            cdf = new CDFPTKAndroidImpl(mUniSDKInstance.getContext(), handler);
            cdf.PTK_ConnectWiFi(ip, port);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", 1);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "PTK_ConnectWiFi: " + e);
            e.printStackTrace();
        }

    }


    //2.2 断开 WiFi
    @UniJSMethod(uiThread = false)
    public void PTK_CloseConnectWiFi(JSONObject options, UniJSCallback callback) {
        try {
            cdf.PTK_CloseConnectWiFi();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", 1);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "PTK_CloseConnectWiFi: " + e);
            e.printStackTrace();
        }
    }

    //2.5 清除缓存
    //1.此函数不能在 FORM 的编排过程中使用。
    //2.打印机缓存中的 FORM 和图形不会被清空。
    @UniJSMethod(uiThread = false)
    public void PTK_ClearBuffer(JSONObject options, UniJSCallback callback) {
        try {
            int ret = cdf.PTK_ClearBuffer();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "PTK_ClearBuffer: " + e);
            e.printStackTrace();
        }
    }

    //2.6 设置打印速度
    @UniJSMethod(uiThread = false)
    public void PTK_SetPrintSpeed(JSONObject options, UniJSCallback callback) {
        try {
            int px = options.getIntValue("px");
            int ret = cdf.PTK_SetPrintSpeed(px);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "PTK_SetPrintSpeed: " + e);
            e.printStackTrace();
        }
    }


    //2.7 设置标签打印方向
    //direct：标签的打印方向。
    //B - 从标签右下角开始打印；
    //T - 从标签左上角开始打印。
    @UniJSMethod(uiThread = false)
    public void PTK_SetDirection(JSONObject options, UniJSCallback callback) {
        try {
            String direct = options.getString("direct");
            int ret = cdf.PTK_SetDirection(direct);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "PTK_SetDirection: " + e);
            e.printStackTrace();
        }
    }

    //2.8 设置打印黑度
    //id：打印黑度。取值范围：0～20。
    @UniJSMethod(uiThread = false)
    public void PTK_SetDarkness(JSONObject options, UniJSCallback callback) {
        try {
            int id = options.getIntValue("id");
            int ret = cdf.PTK_SetDarkness(id);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "PTK_SetDarkness: " + e);
            e.printStackTrace();
        }
    }


    //2.9
    @UniJSMethod(uiThread = false)
    public void PTK_SetLabelHeight(JSONObject options, UniJSCallback callback) {
        try {
            int lHeight = options.getIntValue("lHeight");
            int gapH = options.getIntValue("gapH");
            int gapOffset = options.getIntValue("gapOffset");
            boolean bFlag = options.getBoolean("bFlag");
            int ret = cdf.PTK_SetLabelHeight(lHeight, gapH, gapOffset, bFlag);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "PTK_SetDarkness: " + e);
            e.printStackTrace();
        }
    }

    //2.10
    @UniJSMethod(uiThread = false)
    public void PTK_SetLabelWidth(JSONObject options, UniJSCallback callback) {
        try {
            int lWidth = options.getIntValue("lWidth");
            int ret = cdf.PTK_SetLabelWidth(lWidth);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "PTK_SetDarkness: " + e);
            e.printStackTrace();
        }
    }

    //2.11
    @UniJSMethod(uiThread = false)
    public void PTK_PrintLabel(JSONObject options, UniJSCallback callback) {
        try {
            int number = options.getIntValue("number");
            int cpnumber = options.getIntValue("cpnumber");
            int ret = cdf.PTK_PrintLabel(number, cpnumber);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "PTK_SetDarkness: " + e);
            e.printStackTrace();
        }
    }

    @UniJSMethod(uiThread = false)
    public void PTK_PrintLabelAuto(JSONObject options, UniJSCallback callback) {
        try {
            int number = options.getIntValue("number");
            int cpnumber = options.getIntValue("cpnumber");
            int ret = cdf.PTK_PrintLabelAuto(number, cpnumber);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "PTK_SetDarkness: " + e);
            e.printStackTrace();
        }
    }


    @UniJSMethod(uiThread = false)
    public void PTK_SoftFontList(JSONObject options, UniJSCallback callback) {
        try {
            int ret = cdf.PTK_SoftFontList();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "PTK_SoftFontList: " + e);
            e.printStackTrace();
        }
    }

    //2.14
    @UniJSMethod(uiThread = false)
    public void PTK_SoftFontDel(JSONObject options, UniJSCallback callback) {
        try {
            int id = options.getIntValue("id");
            int ret = cdf.PTK_SoftFontDel((char) id);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "PTK_SoftFontDel: " + e);
            e.printStackTrace();
        }
    }

    //2.15
    @UniJSMethod(uiThread = false)
    public void PTK_EnableBackFeed(JSONObject options, UniJSCallback callback) {
        try {
            int ret = cdf.PTK_EnableBackFeed();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "PTK_EnableBackFeed: " + e);
            e.printStackTrace();
        }
    }

    //2.16
    @UniJSMethod(uiThread = false)
    public void PTK_DisableBackFeed(JSONObject options, UniJSCallback callback) {
        try {
            int ret = cdf.PTK_DisableBackFeed();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, " PTK_DisableBackFeed: " + e);
            e.printStackTrace();
        }
    }

    @UniJSMethod(uiThread = false)
    public void PTK_PrintConfiguration(JSONObject options, UniJSCallback callback) {
        try {
            int ret = cdf.PTK_PrintConfiguration();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, " PTK_PrintConfiguration: " + e);
            e.printStackTrace();
        }
    }

    @UniJSMethod(uiThread = false)
    public void PTK_SetPrinterState(JSONObject options, UniJSCallback callback) {
        try {
            int state = options.getIntValue("state");
            int ret = cdf.PTK_SetPrinterState((char) state);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, " PTK_PrintConfiguration: " + e);
            e.printStackTrace();
        }
    }

    @UniJSMethod(uiThread = false)
    public void PTK_EnableFLASH(JSONObject options, UniJSCallback callback) {
        try {
            int ret = cdf.PTK_EnableFLASH();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "  PTK_EnableFLASH: " + e);
            e.printStackTrace();
        }
    }

    @UniJSMethod(uiThread = false)
    public void PTK_DisableFLASH(JSONObject options, UniJSCallback callback) {
        try {
            int ret = cdf.PTK_DisableFLASH();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "  PTK_DisableFLASH: " + e);
            e.printStackTrace();
        }
    }

    @UniJSMethod(uiThread = false)
    public void PTK_FeedMedia(JSONObject options, UniJSCallback callback) {
        try {
            int ret = cdf.PTK_FeedMedia();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "PTK_FeedMedia: " + e);
            e.printStackTrace();
        }
    }

    @UniJSMethod(uiThread = false)
    public void PTK_MediaDetect(JSONObject options, UniJSCallback callback) {
        try {
            int ret = cdf.PTK_MediaDetect();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "PTK_FeedMedia: " + e);
            e.printStackTrace();
        }
    }

    @UniJSMethod(uiThread = false)
    public void PTK_Reset(JSONObject options, UniJSCallback callback) {
        try {
            int ret = cdf.PTK_Reset();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, " PTK_Reset : " + e);
            e.printStackTrace();
        }
    }

    @UniJSMethod(uiThread = false)
    public void PTK_CutPage(JSONObject options, UniJSCallback callback) {
        try {
            int page = options.getIntValue("page");
            int ret = cdf.PTK_CutPage(page);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, " PTK_CutPage : " + e);
            e.printStackTrace();
        }
    }

    @UniJSMethod(uiThread = false)
    public void PTK_CutPageEx(JSONObject options, UniJSCallback callback) {
        try {
            int page = options.getIntValue("page");
            int ret = cdf.PTK_CutPageEx(page);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "  PTK_CutPageEx : " + e);
            e.printStackTrace();
        }
    }


    @UniJSMethod(uiThread = false)
    public void PTK_FeedBack(JSONObject options, UniJSCallback callback) {
        try {
            int ret = cdf.PTK_FeedBack();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "   PTK_FeedBack : " + e);
            e.printStackTrace();
        }
    }

    @UniJSMethod(uiThread = false)
    public void PTK_SetNetworkFeedbackParameter(JSONObject options, UniJSCallback callback) {
        try {
            int port = options.getIntValue("port");
            String ip = options.getString("ip");
            int cp = options.getIntValue("cp");
            int ret = cdf.PTK_SetNetworkFeedbackParameter(port, ip, cp);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "   PTK_SetNetworkFeedbackParameter : " + e);
            e.printStackTrace();
        }
    }

    @UniJSMethod(uiThread = false)
    public void PTK_SetFeedbackPort(JSONObject options, UniJSCallback callback) {
        try {
            int port = options.getIntValue("port");
            int ret = cdf.PTK_SetFeedbackPort(port);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "   PTK_SetFeedbackPort : " + e);
            e.printStackTrace();
        }
    }

    @UniJSMethod(uiThread = false)
    public void PTK_DrawText(JSONObject options, UniJSCallback callback) {
        try {
            int px = options.getIntValue("px");
            int py = options.getIntValue("py");
            int pdirec = options.getIntValue("pdirec");
            int pFont = options.getIntValue("pFont");
            int pHorizontal = options.getIntValue("pHorizontal");
            int pVertical = options.getIntValue("pVertical");
            int ptext = options.getIntValue(" ptext");
            String pstr = options.getString("pstr");
            int ret = cdf.PTK_DrawText(px, py, pdirec, (char) pFont, pHorizontal, pVertical, (char) ptext, pstr);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "   PTK_DrawText : " + e);
            e.printStackTrace();
        }
    }

    @UniJSMethod(uiThread = false)
    public void PTK_DrawAndroidText(JSONObject options, UniJSCallback callback) {
        try {
            int px = options.getIntValue("px");
            int py = options.getIntValue("py");
            int pSize = options.getIntValue("pSize");
            int pType = options.getIntValue("pType");
            String ptext = options.getString("ptext");
            String textName = options.getString("textName");
            String pstr = options.getString("pstr");
            int ret = cdf.PTK_DrawAndroidText(px, py, pSize, pType, ptext, textName, pstr);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", ret);
            callback.invoke(jsonObject);
        } catch (Exception e) {
            Log.e(TAG, "   PTK_DrawText : " + e);
            e.printStackTrace();
        }
    }
}
